<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/code.js"></script>
    <script src="../js/cookie.js"></script>
    <style>
        #createCode {
            user-select: none;
        }

        .right {
            font-size: 12px;
            color: green;
        }

        .err {
            font-size: 12px;
            color: red;
        }
    </style>
</head>

<body>
    <form class="box" onsubmit="return false">
        <p>
            <label for="">账&emsp;号:</label>
            <input type="text" id="user" placeholder="用户名/手机号/邮箱">
            <span id="user_span"></span>
        </p>
        <p>
            <label for="">密&emsp;码:</label>
            <input type="password" id="pwd">
            <span id="pwd_span"></span>
        </p>
        <p>
            <label><input type="checkbox" id="isRem"> 是否记住用户名?</label>
        </p>
        <p>
            <input id="loginBtn" type="submit" value="登录">
            <input id="retBtn" type="reset" value="取消">
        </p>
    </form>
</body>
<script>
    var userInp = document.getElementById("user");
    var pwdInp = document.getElementById("pwd");
    var loginBtn = document.getElementById("loginBtn");
    var isRem = document.getElementById("isRem");

    loginBtn.onclick = function () {
        // 用户输入的
        var account = userInp.value;
        var pwd = pwdInp.value;

        if (account && pwd) {
            var xhr = new XMLHttpRequest();

            xhr.open("post", `../php/login_account.php`, true);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(`account=${account}&pwd=${pwd}`);

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var result = xhr.responseText;
                    console.log(result);
                    var data = JSON.parse(result);
                    console.log(data);
                    var { status, msg, user } = data;
                    if (status) {
                        if (isRem.checked) {
                            setCookie("lgc", user, 31);
                        } else {
                            setCookie("lgc", user);
                        }

                        location.href = "./index1.html";
                        // alert("注册成功");
                    } else {
                        alert(msg);
                    }
                }
            }
        } else {
            alert("请输入完整信息");
        }



    }





</script>


</html>